<!--评价管理-->
<template>
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
         <el-form-item label="商品名" prop="goodsName">
            <el-input
              v-model="queryParams.goodsName"
              placeholder="请输入商品名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="用户名" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:comment:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="commentList">
      <el-table-column label="评论ID" align="center" prop="id" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="用户名" align="center" prop="userName" width="120" :show-overflow-tooltip="true"/>
      <!--      1:普通评论2:商家评论-->
      <el-table-column label="评价来源" align="center" prop="type" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.type == 1 ? '普通评论' : '商家评论'}}
        </template>
      </el-table-column>
      <el-table-column label="商品名" align="left" prop="goodsName" width="600" :show-overflow-tooltip="true"/>
      <el-table-column label="评分" align="center" prop="rating" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.rating == 1 ? '好评' : '差评'}}
        </template>
      </el-table-column>
      <el-table-column label="详情" align="left" prop="content" width="300" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-html="scope.row.content"></span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="评价回复" align="center" prop="content" />
            <el-table-column label="商品ID" align="center" prop="goodsId" />
            <el-table-column label="sku名" align="center" prop="skuName" />
            <el-table-column label="订单ID" align="center" prop="orderId" />
            <el-table-column label="用户ID" align="center" prop="userId" />
            <el-table-column label="用户头像" align="center" prop="userAvatar" />
            <el-table-column label="评论图片" align="center" prop="images" />
            <el-table-column label="父级评论ID" align="center" prop="parentId" />
            <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createdTime, '{m}/{d}/{y}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.updatedTime, '{m}/{d}/{y}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="删除" align="center" prop="deleted" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUBack(scope.row)"
              v-hasPermi="['car:comment:edit']"
            >回复
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['car:comment:remove']"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-setting"
              @click="handleHidden(scope.row)"
            >{{scope.row.enable == '1' ? '隐藏' : '展示'}}
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 回复 -->
    <el-dialog title="回复评论" :visible.sync="openBack" width="500px" append-to-body>
      <el-row class="details-row">
        <el-col :span="4" class="details-label">用户</el-col>
        <el-col :span="20">{{currentDetail.userName}}</el-col>
      </el-row>
      <el-row class="details-row">
        <el-col :span="4" class="details-label">商品名称</el-col>
        <el-col :span="20">{{currentDetail.goodsName}}</el-col>
      </el-row>
      <el-row class="details-row">
        <el-col :span="4" class="details-label">评分</el-col>
        <el-col :span="20">{{currentDetail.rating == '1' ? '好评' : '差评'}}</el-col>
      </el-row>
      <el-row class="details-row">
        <el-col :span="4" class="details-label">详情</el-col>
        <el-col :span="20"><span v-html="currentDetail.content"></span></el-col>
      </el-row>
      <el-form ref="formBack" :model="formBack" label-width="80px">
        <el-form-item label="回复评论" prop="content"  :rules="[{ required: true, message: '请输入回复内容', trigger: 'blur' }]">
          <el-input type="textarea" v-model="formBack.content" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormBack">确 定</el-button>
        <el-button @click="cancelBack">取 消</el-button>
      </div>
    </el-dialog>
    <!--    新增评价-->
    <el-dialog title="补充客户评价" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="评价时间" prop="createdTime">
          <el-date-picker clearable
                          v-model="form.createdTime"
                          type="date"
                          placeholder="请选择创建时间"
                          value-format="timestamp"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户头像" prop="userAvatar">
          <ImageUpload :limit="1" v-model="form.userAvatar"></ImageUpload>
        </el-form-item>
        <el-form-item label="商品名" prop="goodSkuId">
          <!--          <el-input v-model="form.goodsName" placeholder="请输入商品名"/>-->
          <el-select v-model="form.goodSkuId" placeholder="请选择" v-loadmore="loadmore">
            <el-option
              v-for="item in productsList"
              :key="item.goodSkuId"
              :label="item.skuName"
              :value="item.id"
            >
            </el-option>
          </el-select>
<!--          <remote-select v-model="form.goodSkuId" :remote-api="productsApi" search-key="name" value-key="id"
                         label-key="name" placeholder="请选择" :init-data="productsList"
          />-->
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
<!--          <editor v-model="form.content" :min-height="192"/>-->
          <el-input type="textarea" v-model="form.content" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="评论图片" prop="images">
          <ImageUpload :limit="3" v-model="form.images"></ImageUpload>
        </el-form-item>
        <el-form-item label="评分" prop="rating">
          <el-radio v-model="form.rating" label="1">好评</el-radio>
          <el-radio v-model="form.rating" label="2">差评</el-radio>
        </el-form-item>
        <el-form-item label="是否隐藏" prop="enable">
          <el-radio v-model="form.enable" label="1">显示</el-radio>
          <el-radio v-model="form.enable" label="2">隐藏</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listComment,
  hidden,
  delComment,
  addComment,
  products,
  backComment
} from '@/api/order/comment'
import RemoteSelect from '@/components/RemoteSelect/index.vue'
import Template from '@/views/setting/template/index.vue'
import loadMore from '@/utils/loadMore'
export default {
  name: 'Comment',
  components: { Template, RemoteSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品评论表格数据
      commentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openBack: false,
      // 查询参数
      queryParams: {
        'size': 10,
        'current': 1
      },
      formBack: {},
      // 表单参数
      form: {
        rating: '1',
        enable: '1'
      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        userAvatar: [
          { required: true, message: '请上传', trigger: 'blur' }
        ],
        goodSkuId: [
          { required: true, message: '请选择商品', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '评论内容不能为空', trigger: 'blur' }
        ],
        images: { required: true, message: '请上传评价图片', trigger: 'blur' }
        /*  userAvatar: [
           {required: true, message: "请上传用户头像", trigger: ["change", "blur"]}
         ], */
      },
      productsList:[],
      currentDetail:{},
      pageNum: 1,
      productsTotal:null,
    }
  },
  created() {
    this.getList()
    this.getProducts()
  },
  methods: {
    loadmore(){
      if(this.productsTotal != this.productsList.length){
        this.pageNum++;
        this.getProducts()
      }
    },
    productsApi: products,
    /** 查询商品评论列表 */
    getList() {
      this.loading = true
      listComment(this.queryParams).then(response => {
        this.commentList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 查询商品列表 */
    getProducts() {
      products({ pageSize: 20, pageNum: this.pageNum }).then(response => {
        let resData = []
        resData.push(...response.data.records)
        let skus = [];
        for (let i = 0; i < resData.length ; i++) {
          skus = [...skus,...resData[i].goodsSkus];
        }
        this.productsTotal = response.data.total
        this.productsList.push(...skus);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        goodsName: null,
        userName: null,
        userAvatar: null,
        content: null,
        images: null,
        rating: '1',
        enable: '1',
        parentId: 0,
        createdTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品评论'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getComment(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品评论'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // this.form.createdTime.toString();
          addComment(this.form).then(response => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品评论编号为"' + ids + '"的数据项？').then(function() {
        return delComment(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('car/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`)
    },
    //提交回复
    submitFormBack: function() {
      this.$refs['formBack'].validate(valid => {
        if (valid) {
          // this.form.createdTime.toString();
          let aetData = {
            // id:this.currentDetail.id,
            // goodsId:this.currentDetail.goodsId,
            // content:this.formBack.content
            parentId: this.currentDetail.id,
            content: this.formBack.content
          };
          console.log(aetData);
          backComment(aetData).then(response => {
            this.$modal.msgSuccess('回复成功')
            this.openBack = false
            this.getList();
          })
        }
      })
    },
    //取消回复
    cancelBack: function() {
      this.formBack = {};
      this.openBack = false;
    },
    //回复
    handleUBack: function(row) {
      this.currentDetail = row;
      this.openBack = true;
    },
    //修改隐藏状态
    handleHidden: function(row) {
      this.$modal.confirm('是否确认隐藏商品评论编号为"' + row.id + '"的数据项？').then(function() {
        return hidden(row.id);
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('隐藏成功')
      }).catch(() => {
      })
    },
  }
}
</script>
